package com.skywolf.chem.common.sysconfig.response;

/**
 * Created by pei_pei on 2020/10/27.
 */

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel("通用返回对象")
public final class RespData<T> {
    private static final Integer SUCCESS_CODE = Integer.valueOf(0);
    private static final String SUCCESS_MSG = "SUCCESS";
    @ApiModelProperty(
            value = "code标识符，000000表示成功，详情见ResponseCodeEnum",
            required = true,
            example = "000000"
    )
    private Integer code;
    @ApiModelProperty(
            value = "消息说明",
            required = true,
            example = "SUCCESS"
    )
    private String message;
    @ApiModelProperty("返回的数据json对象")
    private T data;

    public static <T> RespData success(T data) {
        return new RespData(SUCCESS_CODE, "SUCCESS", data);
    }

    public static RespData success() {
        return success((Object)null);
    }

    public static <T> RespData failure(IResponseCode responseCode, T data) {
        return new RespData(responseCode, data);
    }

    public static RespData failure(IResponseCode responseCode) {
        return failure(responseCode, (Object)null);
    }

    private RespData(IResponseCode responseCode, T data) {
        this.code = responseCode.getCode();
        this.message = responseCode.getMessage();
        this.data = data;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return SUCCESS_CODE.equals(this.getCode());
    }

    public RespData() {
    }

    public RespData(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Integer getCode() {
        return this.code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

